package controles;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class IpText extends Composite 
{
	private Text campo1 = null;
	private Text campo2 = null;
	private Text campo3 = null;
	private Text campo4 = null;
	private Label ponto1 = null;
	private Label ponto2 = null;
	private Label ponto3 = null;
	
	public IpText(Composite parent, int style) 
	{
		super(parent, style);
		
		IpText.this.setBackground(new Color(Display.getCurrent(), 255, 255, 255));
		IpText.this.setSize(160, 22);
		
		campo1 = new Text(IpText.this,SWT.CENTER);
		campo1.setLocation(1, 3);
		campo1.setSize(30,16);
		campo1.setTextLimit(3);
		campo1.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
			public void keyPressed(org.eclipse.swt.events.KeyEvent e) 
			{				
				if(e.keyCode==8)
					return;
				
				if(e.keyCode == 32 || e.keyCode == 16777220 || e.keyCode == 46)
				{
					campo2.setFocus();
					e.doit = false;
					return;
				}
				if(campo1.getCaretPosition()==2)
				{
					campo2.setFocus();
					return;
				}
				if(!Character.isDigit(e.keyCode))
				{
					e.doit = false;
				}
			}
		});
		
		ponto1 = new Label(IpText.this,SWT.NONE);
		ponto1.setBackground(new Color(Display.getCurrent(), 255, 255, 255));
		ponto1.setLocation(35, 4);
		ponto1.setText(".");
		ponto1.pack();
		
		campo2 = new Text(IpText.this,SWT.CENTER);
		campo2.setLocation(42, 3);
		campo2.setSize(30,16);
		campo2.setTextLimit(3);
		campo2.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
			public void keyPressed(org.eclipse.swt.events.KeyEvent e) 
			{
				if(e.keyCode==8)
				{
					if(campo2.getCaretPosition()==0)
						campo1.setFocus();
					return;
				}
				if(e.keyCode == 32 || e.keyCode == 16777220 || e.keyCode == 46)
				{
					campo3.setFocus();
					e.doit = false;
					return;
				}
				
				if(e.keyCode == 16777219)
				{
					campo1.setFocus();
					e.doit = false;
					return;
				}
				if(campo2.getCaretPosition()==2)
				{
					campo3.setFocus();
					return;
				}
				if(!Character.isDigit(e.keyCode))
				{
					e.doit = false;
				}
			}
		});
		
		ponto2 = new Label(IpText.this,SWT.NONE);
		ponto2.setBackground(new Color(Display.getCurrent(), 255, 255, 255));
		ponto2.setLocation(76, 4);
		ponto2.setText(".");
		ponto2.pack();
		
		campo3 = new Text(IpText.this,SWT.CENTER);
		campo3.setLocation(83, 3);
		campo3.setSize(30,16);
		campo3.setTextLimit(3);
		campo3.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
			public void keyPressed(org.eclipse.swt.events.KeyEvent e) 
			{
				if(e.keyCode==8)
				{
					if(campo3.getCaretPosition()==0)
						campo2.setFocus();
					return;
				}
				
				if(e.keyCode == 32 || e.keyCode == 16777220 || e.keyCode == 46)
				{
					campo4.setFocus();
					e.doit = false;
					return;
				}
				
				if(e.keyCode == 16777219)
				{
					campo2.setFocus();
					e.doit = false;
					return;
				}
				
				if(campo3.getCaretPosition()==2)
				{
					campo4.setFocus();
					return;
				}
				if(!Character.isDigit(e.keyCode))
				{
					e.doit = false;
				}
			}
		});
		
		ponto3 = new Label(IpText.this,SWT.NONE);
		ponto3.setBackground(new Color(Display.getCurrent(), 255, 255, 255));
		ponto3.setLocation(117, 4);
		ponto3.setText(".");
		ponto3.pack();
		
		campo4 = new Text(IpText.this,SWT.CENTER);
		campo4.setLocation(124, 3);
		campo4.setSize(30,16);
		campo4.setTextLimit(3);
		campo4.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
			public void keyPressed(org.eclipse.swt.events.KeyEvent e) 
			{
				if(e.keyCode == 46 || e.keyCode == 32)
				{
					e.doit = false;
					return;
				}
				
				if(e.keyCode==8)
				{
					if(campo4.getCaretPosition()==0)
						campo3.setFocus();
					return;
				}
				
				if(e.keyCode == 16777219)
				{
					campo3.setFocus();
					e.doit = false;
					return;
				}
				if(!Character.isDigit(e.keyCode))
				{
					e.doit = false;
				}
			}
		});
	}
	public void setEnabled(boolean opcao)
	{
		if(opcao == false)
		{
			IpText.this.setBackground(new Color(Display.getCurrent(),234,234,234));
			campo1.setBackground(new Color(Display.getCurrent(),234,234,234));
			campo2.setBackground(new Color(Display.getCurrent(),234,234,234));
			campo3.setBackground(new Color(Display.getCurrent(),234,234,234));
			campo4.setBackground(new Color(Display.getCurrent(),234,234,234));
			ponto1.setBackground(new Color(Display.getCurrent(),234,234,234));
			ponto2.setBackground(new Color(Display.getCurrent(),234,234,234));
			ponto3.setBackground(new Color(Display.getCurrent(),234,234,234));
			campo1.setEnabled(false);
			campo2.setEnabled(false);
			campo3.setEnabled(false);
			campo4.setEnabled(false);
			return;
		}
		IpText.this.setBackground(new Color(Display.getCurrent(),255,255,255));
		campo1.setBackground(new Color(Display.getCurrent(),255,255,255));
		campo2.setBackground(new Color(Display.getCurrent(),255,255,255));
		campo3.setBackground(new Color(Display.getCurrent(),255,255,255));
		campo4.setBackground(new Color(Display.getCurrent(),255,255,255));
		ponto1.setBackground(new Color(Display.getCurrent(),255,255,255));
		ponto2.setBackground(new Color(Display.getCurrent(),255,255,255));
		ponto3.setBackground(new Color(Display.getCurrent(),255,255,255));
		campo1.setEnabled(true);
		campo2.setEnabled(true);
		campo3.setEnabled(true);
		campo4.setEnabled(true);
	}
	public void clear()
	{
		campo1.setText("");
		campo2.setText("");
		campo3.setText("");
		campo4.setText("");
	}
	public String getText()
	{
		return campo1.getText() + "." + campo2.getText() + "." + campo3.getText() + "." + campo4.getText();
	}
}
